package org.vectrics.recruiter.xmlweb;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.vectrics.SystemException;
import org.vectrics.dataweb.WebServiceAction;
import org.vectrics.location.Location;
import org.vectrics.location.LocationCriteria;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.Request;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class LocationSearchWebService extends WebServiceAction {

	public static String COMMAND = "get.matching.locations";
	
	@Override
	public void getData(Request request, String command,
			Element requestElement, Element responseElement) {
		log.info("Command: " + command);
		Element dataElement = this.getDataElement(responseElement);
		if (command.equals(COMMAND)) {
			LocationCriteria criteria = new LocationCriteria();
			log.info("Search location name: " + request.getParameter("name"));
			
			criteria.setName("%"+request.getParameter("name"));
			
			if (request.getParameter("typeCode")!= null) {
				criteria.setLocationType(request.getParameter("typeCode"));
			}
			
			Collection<Location> locations = CoreServiceLocator.getLocationService().findLocations(criteria, 0, 100);
			List locationsPadres = new ArrayList<Location>();
			
			for (Location location : locations ) {
				while (location.getParent()!= null) {
					location = location.getParent();
				}
				if (!locationsPadres.contains(location)) {
					locationsPadres.add(location);
				}
			}
			
			displayLocation(locations, locationsPadres, dataElement, responseElement);
			
		} else {
			throw new SystemException("Do not know how to handle command: " + command);
		}
	}
	
	private void displayLocation(Collection<Location> locs, Collection<Location> loc, Node dataElement, Node responseElement) {
		List locations = new ArrayList();
		
		for (Location l : loc) {
			locations.add(l);
		}
		
		Collections.sort(locations);
		
		Iterator iter = locations.iterator();
		
		while (iter.hasNext()) {
			Location location = (Location)iter.next();
			log.info("Location search - found location: " + location);
			// Agrego el Nodo
			if (deboMostrar(locs,location)){				
				Element locationElement = responseElement.getOwnerDocument().createElement("location");
				this.locationToElement(location, locationElement);
				dataElement.appendChild(locationElement);
				
				if ((location.getChildren() != null) && (!location.getChildren().isEmpty())) {
					displayLocation(locs, location.getChildren(), dataElement,  responseElement);
				}
			}
		}
	}
	
	private boolean deboMostrar(Collection<Location> locs, Location location) {
		boolean resultado = false;
		
		if (location.getParent()==null){
			return true;
		}
		
		if (locs.contains(location)) {
			return true;
		} else {
			if ((location.getChildren() != null) && (!location.getChildren().isEmpty())) {
				Iterator iter = location.getChildren().iterator();
				while (iter.hasNext()) {
					if  (deboMostrar(locs, (Location) iter.next())) {
						resultado=true;
						break;
					}
				}				
			}
		}
		return resultado;
	}

	private void locationToElement(Location location, Element locationElement) {
		locationElement.setAttribute("id", location.getId().toString());
		locationElement.setAttribute("description", location.getDescription());
		locationElement.setAttribute("typeCode", location.getTypeCode());
		locationElement.setAttribute("name", location.getName());
		locationElement.setAttribute("domain", location.getDomain().getId().toString());
	}
	
}
